package com.my.study.aqs;

import com.my.study.utils.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/6/20 17:00
 **/
@Slf4j(topic = "c.TestCountDownLatch")
public class TestCountDownLatch {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        ExecutorService service = Executors.newFixedThreadPool(10);
        final String[] all = new String[10];
        for (int j = 0; j < 10; j++) {
            int k = j;
            service.submit(() -> {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    all[k] = i + "%";
                    System.out.print("\r" + Arrays.toString(all));
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("\n游戏开始...");
        service.shutdown();
    }

    private static void test02() {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        ExecutorService pool = Executors.newFixedThreadPool(4);
        pool.execute(() -> {
            log.debug("begin...");
            ThreadUtil.sleep(1);
            log.debug("end...");
            countDownLatch.countDown();
        });

        pool.execute(() -> {
            log.debug("begin...");
            ThreadUtil.sleep(2);
            log.debug("end...");
            countDownLatch.countDown();
        });

        pool.execute(() -> {
            log.debug("begin...");
            ThreadUtil.sleep(3);
            log.debug("end...");
            countDownLatch.countDown();
        });

        pool.execute(() -> {
            log.debug("waiting...");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("finish...");
        });
    }

    private static void test01() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(() -> {
            log.debug("begin...");
            ThreadUtil.sleep(1);
            log.debug("end...");
            countDownLatch.countDown();
        }, "t1").start();

        new Thread(() -> {
            log.debug("begin...");
            ThreadUtil.sleep(2);
            log.debug("end...");
            countDownLatch.countDown();
        }, "t2").start();

        new Thread(() -> {
            log.debug("begin...");
            ThreadUtil.sleep(3);
            log.debug("end...");
            countDownLatch.countDown();
        }, "t3").start();

        log.debug("waiting...");
        countDownLatch.await();
        log.debug("finish...");
    }

}
